package com.hxzy.dao.impl;

import com.hxzy.dao.BookTypeDao;
import com.hxzy.util.DbHelper;
import entity.BookType;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;
import java.util.Optional;

public class BookTypeDaoImpl extends DbHelper implements BookTypeDao {
    @Override
    public int insert(BookType bookType) {
        String sql="insert into booktype ('type','parent_id') values(?,?)";
        Object[] objects={
          bookType.getType(),
          bookType.getParent()
        };
        return super.executeUpdate(sql,objects);
    }

    @Override
    public int updateByPrimaryKey(BookType bookType) {
        String sql="update booktype set 'type'=?,'parent_id'=? where 'id'=?";
        Object[] objects={
                bookType.getType(),
                bookType.getParent(),
                bookType.getId()
        };

        return super.executeUpdate(sql,objects);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        String sql="delete from booktype where 'id'=?";
        return super.executeUpdate(sql,id);
    }

    @Override
    public Optional<BookType> selectByPrimaryKey(Integer id) {
        String sql="select 'id','type','parent_id' parent from booktype where 'id'=?";
        BeanHandler<BookType> handler = new BeanHandler<>(BookType.class);
        Optional<BookType> optional = super.executeQuery(sql, handler, id);
        return optional;

    }
}
